﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Configuration;


namespace Testify
{
    public class Utilities
    {
        //Returns the given users permission.
        public static int getUserPermission(string p_UserID)
        {
            int permission = 100;
            String queryString1 = @"SELECT t.permission_level FROM testify_user u, user_type t
                                     WHERE u.user_type_id = t.user_type_id
                                       AND u.user_id = :user_id";
            OracleConnection myConnection1 = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            OracleCommand myCommand1 = new OracleCommand(queryString1, myConnection1);
            myCommand1.BindByName = true;
            OracleDataReader myReader1 = default(OracleDataReader);

            try
            {
               myConnection1.Open();

               myCommand1.Parameters.Clear();
               myCommand1.Parameters.Add("user_id", p_UserID);
               myReader1 = myCommand1.ExecuteReader();

               if (myReader1.HasRows)
               {
                  myReader1.Read();
                  permission = int.Parse(myReader1["permission_level"].ToString());
               }
            }
            catch (Exception)
            {
            }
            finally
            {
               myConnection1.Close();
            }

            return permission;
        }

        public static int getUserTypePermission(string p_UserTypeName)
        {
            int permission = 100;
            String queryString1 = @"SELECT permission_level FROM user_type
                                     WHERE user_type_title = :p_userType";
            OracleConnection myConnection1 = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            OracleCommand myCommand1 = new OracleCommand(queryString1, myConnection1);
            myCommand1.BindByName = true;
            OracleDataReader myReader1 = default(OracleDataReader);

            try
            {
               myConnection1.Open();

               myCommand1.Parameters.Clear();
               myCommand1.Parameters.Add("p_userType", p_UserTypeName.Trim());
               myReader1 = myCommand1.ExecuteReader();

               if (myReader1.HasRows)
               {
                  myReader1.Read();
                  permission = int.Parse(myReader1["permission_level"].ToString());
               }
            }
            catch (Exception)
            {
            }
            finally
            {
               myConnection1.Close();
            }

            return permission;
        }

        public static bool logError(string p_PageName, Exception p_Error)
        {
           String queryString1 = @"INSERT INTO error_log (page_name, error_msg, error_StackTrace)
                                                  values (:page_name, :error_msg, :error_StackTrace)";
           OracleConnection myConnection1 = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
           OracleCommand myCommand1 = new OracleCommand(queryString1, myConnection1);
           myCommand1.BindByName = true;

           try
           {
              myConnection1.Open();

              myCommand1.Parameters.Clear();
              myCommand1.Parameters.Add("page_name", p_PageName.Trim());
              if (p_Error.Message.StartsWith("Exception of type "))
              {
                 myCommand1.Parameters.Add("error_msg", p_Error.InnerException.Message.Trim());
                 myCommand1.Parameters.Add("error_StackTrace", p_Error.InnerException.StackTrace.Trim());
              }
              else
              {
                 myCommand1.Parameters.Add("error_msg", p_Error.Message.Trim());
                 myCommand1.Parameters.Add("error_StackTrace", p_Error.StackTrace.Trim());
              }
              
              myCommand1.ExecuteNonQuery();

              return true;
           }
           catch (Exception)
           {
              return false;
           }
           finally
           {
              myConnection1.Close();
           }
        }

    }
}